﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Kcadastro.classes;

namespace Kcadastro.adm
{
    public partial class vms_list : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (this.Master is kcadastro)
            {
                ((kcadastro)this.Page.Master).SelecionaPagina(FW.utils.enumPaginas.Servidores);
            }
            masterPageKcad m = Master as masterPageKcad;
            m.idFerr = 6;
            if (!IsPostBack)
            {
                carregarLista();
            }
            bt_apagar.Enabled = false;
        }

        private void carregarLista()
        {
            servidores servs = new servidores();
            gv_lista.DataSource = servs.listaMaquinas();
            gv_lista.DataBind();

            gv_ws.DataSource = servs.listaRotas();
            gv_ws.DataBind();

            servs = null;
        }

        protected void gv_lista_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            servidores servs = new servidores();
            gv_lista.PageIndex = e.NewPageIndex;
            gv_lista.DataSource = servs.listaMaquinas();
            gv_lista.DataBind();
            servs = null;
        }

        protected void gv_lista_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            if (e.CommandName.ToString() == "visualizar")
            {
                int ind = Convert.ToInt32(e.CommandArgument);
                DataKey data = gv_lista.DataKeys[ind];
                //Session.Add("IDuuser", data.Values["PK_IDuser"].ToString());
                //Response.Redirect("usuarios_edit.aspx", true); //?id=" + data.Values["PK_IDos"].ToString()
                servidores servs = new servidores();
                tb_VM tbv = servs.consultaVM(Convert.ToInt16(data.Values["PK_IDmaq"]));
                tbi_maq.Text = tbv.PK_IDmaq.ToString();
                tb_local.Text = tbv.local;
                tb_nome.Text = tbv.nome;
                tb_ordem.Text = tbv.ordem.ToString();
                tb_url.Text = tbv.url;
                tb_wsfim.Text = tbv.wsfim.ToString();
                tb_wsini.Text = tbv.wsini.ToString();
                ck_ativa.Checked = (bool)tbv.ativa ? true : false;
                ck_novo.Checked = false;
                bt_apagar.Enabled = true;
            }
        }

        protected void bt_gravarMaq_Click(object sender, EventArgs e)
        {
            tb_VM tvm = new tb_VM();
            if (ck_novo.Checked)
            {
                tvm.PK_IDmaq = 0;
            }
            else
            {
                tvm.PK_IDmaq = Convert.ToInt16(tbi_maq.Text);
            }
            tvm.ativa = ck_ativa.Checked;
            tvm.local = tb_local.Text;
            tvm.nome = tb_nome.Text;
            tvm.ordem = Convert.ToInt16(tb_ordem.Text);
            tvm.wsfim = Convert.ToInt16(tb_wsfim.Text);
            tvm.wsini = Convert.ToInt16(tb_wsini.Text);
            tvm.qtdvms = tvm.wsfim - tvm.wsini + 1;
            tvm.url = tb_url.Text;
            servidores servs = new servidores();
            servs.gravarMaquina(tvm);
            servs = null;

            tbi_maq.Text = "";
            tb_local.Text = "";
            tb_nome.Text = "";
            tb_ordem.Text = "";
            tb_url.Text = "";
            tb_wsfim.Text = "";
            tb_wsini.Text = "";
            ck_ativa.Checked = false;
            bt_apagar.Enabled = false;
            carregarLista();

        }

        protected void gv_ws_RowCommand(object sender, GridViewCommandEventArgs e)
        {

        }

        protected void bt_reconstruir_Click(object sender, EventArgs e)
        {
            servidores servs = new servidores();
            servs.reconstruirRotas();
            servs = null;

            carregarLista();
        }

        protected void bt_apagar_Click(object sender, EventArgs e)
        {
            servidores servs = new servidores();
            servs.apagarVM(Convert.ToInt16(tbi_maq.Text));
            servs = null;

            tbi_maq.Text = "";
            tb_local.Text = "";
            tb_nome.Text = "";
            tb_ordem.Text = "";
            tb_url.Text = "";
            tb_wsfim.Text = "";
            tb_wsini.Text = "";
            ck_ativa.Checked = false;
            bt_apagar.Enabled = false;
            carregarLista();
        }
    }
}